﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hsp.Media.Model.Utility
{
    public class EntityHelper
    {

        /// <summary>
        /// 获取表字段修正后的名称
        /// </summary>
        /// <param name="columnName">字段名称</param>
        /// <returns></returns>
        public static string GetEntityName(string columnName)
        {
            var name = columnName;
            if (name.IndexOf("_") > -1)
            {
                name = string.Empty;
                var arr = columnName.Split('_');
                foreach (string s in arr)
                {
                    name += ToUpperFirst(s, 1);
                }
            }
            else
            {
                name = ToUpperFirst(name, 0);
            }
            return name;
        }

        /// <summary>
        /// 字符串首字符大写
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="change">是否预先变更为小写：0-否；1-是；</param>
        /// <returns></returns>
        private static string ToUpperFirst(string str, int change)
        {
            if (string.IsNullOrEmpty(str)) return "";

            //if (str.ToLower() == "qq") str = "Qq";
            //if (str.ToLower() == "id") str = "Id";
            //if (str.ToLower() == "pid") str = "Pid";
            //if (str.Contains("QQ")) str = str.Replace("QQ", "Qq");
            //if (str.Contains("ID")) str = str.Replace("ID", "Id");
            //if (str.Contains("API")) str = str.Replace("API", "Api");

            if (change == 1) str = str.ToLower();
            char[] a = str.ToCharArray();
            a[0] = char.ToUpper(a[0]);
            return new string(a);
        }
    }
}
